<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class fction extends MX_Controller{
        
        protected $_templates;
        protected $_module_name = "";
        
        function __construct() {
            parent::__construct();

            $this->_module_name = $this->router->fetch_module();
            $this->pre_message ="";
            $this->lang->load('access');
            $this->load->model('fction_model','fction');
            //$this->permit_library->check_permit();
            $this->session->set_userdata(array('Url'=>uri_string()));          
        }
        function index($module_id,$page=0){
            $this->acl->check('view','','',base_url());
            
            $data['title'] = lang('function.list');
            $data['page']  = (int)$page;
            if($this->acl->check('add'))
                $data['add']= $this->_module_name."/fction/add/".$module_id; 
            
            $data['_module_name']=$this->_module_name;
            $data['list'] = $this->fction->getFunctionByModule($module_id);
            $data['num'] = count($data['list']);
            $data['module_id']=$module_id;
            $this->_templates['page'] = 'fction/list';
            $this->site_lib->load($this->_templates['page'],$data);
        }
        
        function data_fction(){
          $data=array();
          $data['module_id']        = $this->input->post('module_id');
          $data['function_code']    = $this->input->post('function_code');
          $data['function_name']    = $this->input->post('function_name');
          $data['function_comment'] = $this->input->post('function_comment');

          return $data;
        }

        /*
         * @author : Vo Van Hieu
         * @method : add new function
         * @date   : 19/10/2012
         * @modify : 
         */
        function add($module_id){
            $this->acl->check('add','','',base_url());
            
            $data['title'] = lang('function.add');
            $data['module_id']=$module_id;
            
            if(!empty($_POST['bt_submit'])){
               $this->form_validation->set_rules('module_id',lang('function.module'),'required');
               $this->form_validation->set_rules('function_code',lang('function.code'),'trim|required');
               $this->form_validation->set_rules('function_name',lang('function.name'),'trim|required'); 
               if($this->form_validation->run() == FALSE){
                  $this->pre_message = validation_errors();
               }else{
                 
                    $data_fction=$this->data_fction();
                    if($this->fction->saveData(0,$data_fction)){
                       $this->session->set_flashdata('message',lang('admin.save_successful'));
                       redirect($this->_module_name.'/fction/index/'.$module_id);
                    }else{
                       $this->pre_message = lang('admin.save_unsuccessful');
                       redirect($this->_module_name.'/fction/add/'.$module_id);
                    }
                  
               } 
            }
            $data['modules']=$this->fction->getAllModule();
            
            $data['message'] = $this->pre_message;          
            $this->_templates['page'] = 'fction/add';
            $this->site_lib->load($this->_templates['page'],$data);
        }
          /*
         * @author : Vo Van Hieu
         * @method : edit function
         * @date   : 19/10/2012
         * @modify : 
         */
        function edit($module_id,$id){
          $this->acl->check('edit','','',base_url());  
            
          $data['title'] = lang('function.edit');
          $data['module_id']=$module_id;
          
          $data['rs'] = $this->fction->getFunctionByID($id);
          if(!empty($_POST['bt_submit'])){
             
             $this->form_validation->set_rules('module_id',lang('function.module'),'required');
             $this->form_validation->set_rules('function_code',lang('function.code'),'trim|required');
             $this->form_validation->set_rules('function_name',lang('function.name'),'trim|required');
             
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
                 $data_fction=$this->data_fction();
                if($this->fction->saveData($id,$data_fction)){
                    $this->session->set_flashdata('message',lang('admin.save_successful'));
                    redirect($this->_module_name.'/fction/index/'.$module_id);
                }else{
                    $this->pre_message = lang('admin.save_unsuccessful');
                    redirect($this->_module_name.'/fction/'.$module_id.'/'.$id);
                }
             } 
          }
          
          $data['modules']=$this->fction->getAllModule();
          $data['message'] = $this->pre_message; 
          
          $this->_templates['page'] = 'fction/edit';
          $this->site_lib->load($this->_templates['page'],$data);
        }  
        /*
         * @author : Vo Van Hieu
         * @method : delete function
         * @date   : 19/10/2012
         * @modify : 
         */
        function del($module_id,$id){
            $this->acl->check('del','','',base_url());
            //$page = (int)$this->input->post('page');

            if($this->fction->delete($id))
                $this->session->set_flashdata('message',lang('admin.delete_successful'));
            else 
                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));

            redirect($this->_module_name.'/fction/index/'.$module_id);
        }
        /*
         * @author : Vo Van Hieu
         * @method : delete functions
         * @date   : 19/10/2012
         * @modify : 
         */
        function dels($module_id){
            $this->acl->check('dels','','',base_url());
            
            if(!empty($_POST['ar_id']))
            {
                //$page = (int)$this->input->post('page');
                $ar_id = $this->input->post('ar_id');

                if(!empty($_POST['btn_submit']))
                {
                    for($i = 0; $i < sizeof($ar_id); $i ++) {
                        if ($ar_id[$i]){
                            if($this->fction->delete($ar_id[$i]))
                                $this->session->set_flashdata('message',lang('admin.delete_successful'));
                            else 
                                $this->session->set_flashdata('message',lang('admin.delete_unsuccessful'));
                        }
                    }
                }
            }
            redirect($this->_module_name.'/fction/index/'.$module_id);
        }
    }
/* End of file fction.php */
/* Location: ./application/webadmin/modules/system/controllers/fctioni.php */
